#include <libyuv.h>
#include <malloc.h>
#include "yuvutils.h"

using namespace libyuv;

uint8_t **argb_to_yuv(uint8_t* argbs, int width, int height)
{

    int
            src_stride_frame = width*4,
            dst_stride_y = width,
            dst_stride_u = (width+1)/2,
            dst_stride_v = (width+1)/2;

    uint8_t* dst_y;
    uint8_t* dst_u;
    uint8_t* dst_v;

    size_t data_size = width * height * sizeof(uint8_t*);
    dst_y = static_cast<uint8_t *>(malloc(data_size));
    dst_u = static_cast<uint8_t *>(malloc(data_size));
    dst_v = static_cast<uint8_t *>(malloc(data_size));
    /*for (int j = 0; j < data_size; j++) {
        uint8_t value = argbs[j];
        printf("%d : %d\n", j, value);
    }*/
    ARGBToI420(argbs, src_stride_frame, dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v, dst_stride_v, width, height);
    uint8_t **dst_yuv;
    dst_yuv = static_cast<uint8_t **>(malloc(data_size * 3));
    /*for(int i=0; i<width*height; i++)
    {
        dst_yuv[0][i] = dst_y[i];
        dst_yuv[1][i] = dst_u[i];
        dst_yuv[2][i] = dst_v[i];
    }*/

    /* dst_yuv[0] = static_cast<uint8_t *>(malloc(data_size));
    dst_yuv[1] = static_cast<uint8_t *>(malloc(data_size));
    dst_yuv[2] = static_cast<uint8_t *>(malloc(data_size));*/
    dst_yuv[0] = dst_y;
    dst_yuv[1] = dst_u;
    dst_yuv[2] = dst_v;
    /*int x,y;
    for (int i = 0; i < 100; i++) {
        x = i % width;
        y = i / width;
        printf("data[%d] : [x:%d, y:%d] : [r:%d, g:%d, b:%d]\n", i, x, y, dst_y[i], dst_u[i], dst_v[i]);
    }*/

    return dst_yuv;
}